
sd_color
	var
		name		// the name of the color
		red = 0	// red componant of the color
		green = 0	// green componant of the color
		blue = 0	// red componant of the color
		html		// html string for the color
		icon/Icon	// contains the icon produced by the rgb2icon() proc

	proc
		brightness()
		/* returns the grayscale brightness of the RGB colors. */
			return round((red*30 + green*59 + blue*11)/100,1)

		html2rgb()
		/* Calculates the rgb colors from the html colors */
			red = sd_base2dec(copytext(html,1,3))
			green = sd_base2dec(copytext(html,3,5))
			blue = sd_base2dec(copytext(html,5,7))

		rgb2html()
		/* Calculates the html color from the rbg colors */
			html = sd_dec2base(red,,2) + sd_dec2base(green,,2) + sd_dec2base(blue,,2)
			return html



	New()
		..()
		// if this is an unnamed subtype, name it according to it's type
		if(!name)
			name = "[type]"
			var/slash = sd_findlast(name,"/")
			if(slash)
				name = copytext(name,slash+1)
			name = sd_replacetext(name,"_"," ")

		if(html)	// if there is an html string
			html2rgb()	// convert the html to red, green, & blue values
		else
			rgb2html()	// convert the red, green, & blue values to html
proc
	sd_htmlremove(T as text)
	/* Returns the text string with all potential html tags (anything
		between < and >) removed. */
		T = sd_replacetext(T, "&nbsp;","")
		T = sd_replacetext(T, ";","") //Added by Jon88 to prevent names that conflict with certain procs
		T = sd_replacetext(T, "&","") //Ditto
		T = sd_replacetext(T, ",","") //Read above
		var/open = findtext(T,"<")
		while(open)
			var/close = findtext(T,">",open)
			if(close)
				if(close<lentext(T))
					T = copytext(T,1,open)+copytext(T,close+1)
				else
					T = copytext(T,1,open)
				open = findtext(T,"<")
			else
				open = 0
		return T
proc
	sd_base2dec(number as text, base = 16 as num)
	/* Accepts a number in any base (2 to 36) and returns the equivelent
		value in decimal.
		ARGS:
			number	- number to convert as a text string
			base	- number base
		RETURNS:
			decimal value of the number
	*/
		if(!istext(number))
			world.log << "sd_base2dec: invalid number string- [number]"
			return null
		if(!isnum(base) || (base < 2) || (base > 36))
			world.log << "sd_base2dec: invalid base - [base]"
			return null

		var/decimal = 0
		number = uppertext(number)

		for(var/loop = 1, loop <= lentext(number))
			var/digit = copytext(number,loop,++loop)
			if((digit >= "0") && (digit <= "9"))
				decimal = decimal * base + text2num(digit)
			else if((digit >= "A") && (digit <= "Z"))
				decimal = decimal * base + (text2ascii(digit) - 55)
			else
				break	// terminate when it encounters an invalid character

		return decimal


	sd_dec2base(decimal,base = 16 as num,digits = 0 as num)
	/* Accepts a decimal number and returns the equivelent value in the
		new base as a string.
		ARGS:
			decimal	- number to convert
			base	- new number base
			digits	- if output is less than digits, it will add
						preceeding 0s to pad it out
		RETURNS:
			equivelent value in the new base as a string
	*/
		if(istext(decimal)) decimal = text2num(decimal)
		if(!isnum(decimal) || (decimal < 0))
			world.log << "sd_dec2base: invalid decimal number - [decimal]"
			return null
		if(!isnum(base) || (base < 2) || (base > 36))
			world.log << "sd_dec2base: invalid base - [base]"
			return null

		var/text = ""
		if(!decimal) text = "0"
		while(decimal)
			var/n = decimal%base
			if(n<10)
				text = num2text(n) + text
			else
				text = ascii2text(55+n) + text

			decimal = (decimal - n)/base

		while(lentext(text) < digits)
			text = "0" + text

		return text
proc
	sd_findlast(maintext as text, searchtext as text)
	/* Returns the location of the last instance of searchtext in
		maintext. sd_findlast is not case sensitive. */
		var/loc = 0
		var/looking = findtext(maintext, searchtext)
		while(looking)
			loc = looking
			looking = findtext(maintext, searchtext, looking + 1)
		return loc

	sd_findLast(maintext as text, searchtext as text)
	/* Returns the location of the last instance of searchtext in
		maintext. sd_findLast is case sensitive. */
		var/loc = 0
		var/looking = findText(maintext, searchtext)
		while(looking)
			loc = looking
			looking = findText(maintext, searchtext, looking + 1)
		return loc


	sd_replacetext(maintext as text, oldtext as text, newtext as text)
	/* Replaces all instances of oldtext within maintext with newtext.
		sd_replacetext is not case sensitive. See sd_replaceText for a
		case sensitive version. */
		var/F = findtext(maintext, oldtext)
		while(F)
			var/newmessage = copytext(maintext,1,F) + newtext + copytext(maintext,F+lentext(oldtext))
			maintext = newmessage
			F = findtext(maintext, oldtext)
		return maintext

	sd_replaceText(maintext as text, oldtext as text, newtext as text)
	/* Replaces all instances of oldtext within maintext with newtext.
		sd_replaceText is case sensitive. See sd_replacetext for a
		non-case sensitive version. */
		var/F = findText(maintext, oldtext)
		while(F)
			var/newmessage = copytext(maintext,1,F) + newtext + copytext(maintext,F+lentext(oldtext))
			maintext = newmessage
			F = findText(maintext, oldtext)
		return maintext
